package view.userinteface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import view.controller.ControllerPrincipal;
import beans.BeanCliente;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JInternalFrameModificarCliente extends javax.swing.JInternalFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txtCUIT;
	private JTextField txtRazonSocial;
	private JLabel lblCUIT;
	private JLabel lblDNI;
	private JLabel lblTelefono;
	private JTextField txtTelefono;
	private JTextField txtContacto;
	private JButton btnAceptar;
	private JButton jButton1;
	private JButton btnCancelar;
	private JLabel lblNombre;
	private JLabel lblContacto;
	private JTextField txtPorcentajeDescuento;

	/**
	* Auto-generated main method to display this 
	* JInternalFrame inside a new JFrame.
	*/
		
	public JInternalFrameModificarCliente() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(400, 296));
			this.setBounds(0, 0, 400, 296);
			setVisible(true);
			this.setTitle("Modificacion Cliente");
			getContentPane().setLayout(null);
			{
				txtPorcentajeDescuento = new JTextField();
				getContentPane().add(txtPorcentajeDescuento);
				txtPorcentajeDescuento.setBounds(189, 110, 166, 21);
			}
			{
				lblContacto = new JLabel();
				getContentPane().add(lblContacto);
				lblContacto.setText("Porcentaje Descuento");
				lblContacto.setBounds(26, 114, 151, 14);
			}
			{
				txtContacto = new JTextField();
				getContentPane().add(txtContacto);
				txtContacto.setBounds(189, 173, 166, 21);
			}
			{
				txtTelefono = new JTextField();
				getContentPane().add(txtTelefono);
				txtTelefono.setBounds(189, 140, 166, 21);
			}
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(149, 12, 222, 21);
				txtCUIT.setText("Ingrese CUIT de cliente a modificar");
			}
			{
				txtRazonSocial = new JTextField();
				getContentPane().add(txtRazonSocial);
				txtRazonSocial.setBounds(189, 83, 166, 21);
			}
			{
				lblTelefono = new JLabel();
				getContentPane().add(lblTelefono);
				lblTelefono.setText("Contacto");
				lblTelefono.setBounds(26, 177, 151, 14);
			}
			{
				lblDNI = new JLabel();
				getContentPane().add(lblDNI);
				lblDNI.setText("Telefono");
				lblDNI.setBounds(26, 144, 151, 14);
			}
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT: ");
				lblCUIT.setBounds(73, 15, 73, 14);
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Razon Social");
				lblNombre.setBounds(26, 87, 151, 14);
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Cargar Cliente");
				btnAceptar.setBounds(124, 41, 128, 27);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						BeanCliente beanCliente = ControllerPrincipal.getInstancia().buscarClientes(null, txtCUIT.getText());
						if(beanCliente.getCuit()!=null && !"".equalsIgnoreCase(beanCliente.getCuit())){
							txtContacto.setText(beanCliente.getContacto());
							txtPorcentajeDescuento.setText(String.valueOf(beanCliente.getPorcentajeDesc()));
							txtRazonSocial.setText(beanCliente.getRazonSocial());
							txtTelefono.setText(beanCliente.getTelefono());
							txtCUIT.setEditable(false);
						}else{
							JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"Cliente Inexistente");
						}
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(209, 216, 107, 26);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1, "Center");
				jButton1.setText("Aceptar");
				jButton1.setBounds(73, 216, 98, 26);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						txtCUIT.setEditable(true);
						BeanCliente beanCliente = new BeanCliente();
						beanCliente.setContacto(txtContacto.getText());
						beanCliente.setCuit(txtCUIT.getText());
						beanCliente.setRazonSocial(txtRazonSocial.getText());
						beanCliente.setTelefono(txtTelefono.getText());
						Float pjeDto = new Float("0");
						try{pjeDto = Float.parseFloat(txtPorcentajeDescuento.getText());}catch(Exception e){JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"Porcentaje debe ser un numero");return;}
						if(pjeDto>100 || pjeDto<0){
							JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"Porcentaje debe ser un numero entre 0 y 100");
							return;
						}
						beanCliente.setPorcentajeDesc(pjeDto);
						if(ControllerPrincipal.getInstancia().modificarCliente(beanCliente)){
							JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"El cliente fue modificado exitosamente");
							dispose();
						}else{
							JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"El cliente no fue modificado. Hubo un error");
						}
							
					}
				});
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
